
import urllib.request
from bs4 import BeautifulSoup

def create_headers(url):
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Referer':'https://www.mxbc.com/',
        'Cookie': 'Hm_lvt_8004ea3e45a78589c0f7855879726f80=1737688318; HMACCOUNT=CAB23EB560C8DD93; XSRF-TOKEN=eyJpdiI6IjB2bHd2OFh6Q2dCUFBER2l6RzFGamc9PSIsInZhbHVlIjoiZjNaXC9Wb0Rzb0hpMVZVd1RYTTFlSG5hU3dzMUh3c3cydTExK05OMzlxVStSc09hUm9zcFNxMnVrZHdxVUU4SWlmRms2eDFabGV5RmhXTGtySlROMzFmSDNHNlRHeUk0cDdGdG5DN21aN21VcmRRd25iMzdSdWtRaTlzMjlBdlRNIiwibWFjIjoiNTQyM2Q3NjEzY2Q3OGYxMjFmN2UxMjIyOWJkZjk1ODE1OGY1Zjc2MmQ3NjQ1N2FlY2FkZjM1ZTM4NzdjNGQ2YyJ9; sensecms_session=eyJpdiI6IkQrTUtPeUhSdHBIcE80ZnRHalpwK0E9PSIsInZhbHVlIjoiXC9TREtEVWRVOFRBcU1sNktiYjNraEdhS0NtZXJzU05mNnNsSUtYSDB4OFNJU1VpNlZGeXdqVlU3N1h4TXJKVVpNdHZ5SGlRbExnXC9NNkN1UDhvMGZyWHJGXC9mVHR3K1BxM29mWlBmMVFndDlLblhWM1hFeFIycVUyZGdtUGpnaDUiLCJtYWMiOiI5NjBlMDk5MDRmOWY5OWU2MGE1MzM3NDY4MzdmNWE4MTI0MzUxMDZlYjcyNWE5MjkxZTgyZTc2ZWYwZGQ0YjE2In0%3D; Hm_lpvt_8004ea3e45a78589c0f7855879726f80=1737688372',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
    }
    request = urllib.request.Request(url=url,headers=headers)
    return request
def daili(request):
    # 代理
    proxies = {
        'http': '101.200.75.55:80' # 北京
    }
    # handler
    handler = urllib.request.ProxyHandler(proxies=proxies)
    opener = urllib.request.build_opener(handler)
    response = opener.open(request)
    return response

def get_content(response):
    content = response.read().decode('utf-8')
    return content

def jx_bs(html):
    # bs4解析
    soup = BeautifulSoup(html, 'lxml')
    # //ul//li//article/h1/text()
    namelist = soup.select('ul li article h1')
    return namelist


def download(namelist):
    for name in namelist:
        # print(name.get_text())
        name = name.get_text()
        f = open('./download/蜜雪冰城.txt','a+',encoding='utf-8')
        f.write(name+'\n')
    print('下载完成')
    f.close()

if __name__ == '__main__':
    url = 'https://www.mxbc.com/product'
    # 定制请求头
    request = create_headers(url)
    # 代理
    response = daili(request)
    # 网页内容
    html = get_content(response)
    namelist = jx_bs(html)
    download(namelist)








